package org.geektimes.projects.user.web.listener;

import org.geektimes.projects.user.web.ComponentContext;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.sql.DataSource;
import javax.xml.crypto.Data;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Logger;

@WebListener
public class DBConnectionInitializerListener implements ServletContextListener {

    private  ServletContext servletContext;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //1、对当前servlet的上下文ServletContext进行初始化
         servletContext = sce.getServletContext();
         //2、把我们自己的组建上下文初始化
        ComponentContext componentContext = new ComponentContext();
        //3、把我们自己的组建上下文放到ServletContext上下文中
        componentContext.init(servletContext);

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ComponentContext context = ComponentContext.getInstance();
        context.destroy();
    }
}
